<?php

require_once 'MySQLConnect.php';

function escapeQuery( $s )
{
	$mysqli = new mysqli ( 'localhost', 'root', 'fred' );
	
	if ( is_null ( $s ) )
	{
		return "NULL";
	}
	
	return "'" . $mysqli->escape_string ( $s ) . "'";
}

function do_export_table( $database, $table, $type = 'structure' )
{
	$mysqli = new mysqli ( 'localhost', 'root', 'fred' );
	
	switch ( $type )
	{
		case 'structure' :
			$query = $mysqli->query ( "SHOW CREATE TABLE $database.$table" );
			
			$row = mysqli_fetch_row ( $query );
			
			echo "DROP TABLE IF EXISTS `$table`;
			\n $row[1];\n\n";
			break;
		
		case 'both' :
			$exsql = '';
			$max = 100000;
			echo "/*!40000 ALTER TABLE $database.$table DISABLE KEYS */;\n";
			
			$query = $mysqli->query ( "SELECT * FROM $database.$table" );
			
			while ( $row = mysqli_fetch_row ( $query ) )
			{
				$values = '';
				
				foreach ( $row as $v )
				{
					$values .= ( ( $values ) ? ', ' : '' ) . escapeQuery ( $v );
					$exsql .= ( ( $exsql ) ? ', ' : '' ) . " ( " . $values . " ) ";
				
				}
			}
			
			if ( $exsql )
			{
				echo "INSERT INTO $database.$table \n VALUES $exsql;\n";
				echo "/*!40000 ALTER TABLE `$table` ENABLE KEYS */;\n";
				echo "\n";
			}
			break;
		
		default :
			echo 'Choose a type';
			break;
	}
}

do_export_table ( 'test', 'Tutorials', 'structure' );

?>